package com.itheima.mq;

import com.itheima.domain.mongo.VideoScore;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@RocketMQMessageListener(topic = "tanhua-video-161", consumerGroup = "video")
@Component
public class VideoScoreListener implements RocketMQListener<Map> {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onMessage(Map message) {
        //解析传过来的数据
        Long userId = Long.valueOf(message.get("userId").toString());
        Long vid = Long.valueOf(message.get("vid").toString());
        int type = Integer.parseInt(message.get("type").toString());

        //封装实体
        VideoScore videoScore = new VideoScore();
        videoScore.setDate(System.currentTimeMillis());
        videoScore.setUserId(userId);
        videoScore.setVideoId(vid);

        switch (type) {
            case 1:
                videoScore.setScore(20d);
                break;
            case 2:
                videoScore.setScore(5d);
                break;
            case 3:
                videoScore.setScore(-5d);
                break;
            case 4:
                videoScore.setScore(10d);
                break;
        }
        mongoTemplate.save(videoScore);
        System.out.println("推荐视频" + videoScore);
    }
}
